﻿using System;

namespace Basmus.ReadWrite
{
    public class EnumReaderWriter<TEnum> : IReaderWriter<TEnum>
        where TEnum : struct
    {
        public ReadResult<TEnum> Read(string str)
        {
            TEnum value;
            if (Enum.TryParse(str, out value))
            {
                return ReadResult<TEnum>.Success(value);
            }
            return ReadResult<TEnum>.Error();
        }

        public string Write(TEnum value)
        {
            return value.ToString();
        }
    }
}